﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace NPMonitor.Utility
{
    public class HttpActionRequests
    {
        public long Id { get; set; }
        public long ActionId { get; set; }
        public string URL { get; set; }
        public string Method { get; set; }
        public string Result { get; set; }
        public string Error { get; set; }
        public string ServerIP { get; set; }
        public string ServerPort { get; set; }
        public string Started { get; set; }
        public DateTime BeginTime { get; set; }
        public int StatusCode { get; set; }
        /// <summary>
        /// The time taken in seconds to complete this request.  
        /// </summary>
        public double Time { get; set; }
        public int SendBytes { get; set; }
        public int ReceivedBytes { get; set; }

        public double Blocked { get; set; }
        public double CacheRead { get; set; }
        public double Connect { get; set; }
        public double DNSLookup { get; set; }
        public double Network { get; set; }
        public double Receive { get; set; }
        public double Send { get; set; }
        public double TTFB { get; set; }
        public double Wait { get; set; }

        public HttpActionRequests()
        {
            ServerIP = string.Empty;
            ServerPort = string.Empty;
           
        }

        protected string InsertSql
        {
            get
            {
                return string.Format(@"INSERT INTO [NPMonitor].[dbo].[HttpActionRequests]
                                       ([ActionId]
                                       ,[URL]
                                       ,[Method]
                                       ,[Result]
                                       ,[Error]
                                       ,[ServerIP]
                                       ,[ServerPort]
                                       ,[Started]
                                       ,[BeginTime]
                                       ,[StatusCode]
                                       ,[Time]
                                       ,[SendBytes]
                                       ,[ReceivedBytes]
                                       ,[Blocked]
                                       ,[CacheRead]
                                       ,[Connect]
                                       ,[DNSLookup]
                                       ,[Network]
                                       ,[Receive]
                                       ,[Send]
                                       ,[TTFB]
                                       ,[Wait]) VALUES(@ActionId,@URL,@Method,@Result,@Error,@ServerIP,@ServerPort,
                                            @Started,@BeginTime,@StatusCode,@Time,@SendBytes,@ReceivedBytes,
                                            @Blocked,@CacheRead,@Connect,@DNSLookup,@Network,@Receive,@Send,@TTFB,@Wait)");
            }
        }

        public void Insert()
        {
            SqlParameter[] sqlParameters = {
                                                new SqlParameter("@ActionId",ActionId),
                                                new SqlParameter("@URL",URL),
                                                new SqlParameter("@Method",Method),
                                                new SqlParameter("@Result",Result),
                                                new SqlParameter("@Error",Error),
                                                new SqlParameter("@ServerIP",ServerIP),
                                                new SqlParameter("@ServerPort",ServerPort),
                                                new SqlParameter("@Started",Started),
                                                new SqlParameter("@BeginTime",BeginTime),
                                                new SqlParameter("@StatusCode",StatusCode),
                                                new SqlParameter("@Time",Time),
                                                new SqlParameter("@SendBytes",SendBytes),
                                                new SqlParameter("@ReceivedBytes",ReceivedBytes),
                                                new SqlParameter("@Blocked",Blocked),
                                                new SqlParameter("@CacheRead",CacheRead),
                                                new SqlParameter("@Connect",Connect),
                                                new SqlParameter("@DNSLookup",DNSLookup),
                                                new SqlParameter("@Network",Network),
                                                new SqlParameter("@Receive",Receive),
                                                new SqlParameter("@Send",Send),
                                                new SqlParameter("@TTFB",TTFB),
                                                new SqlParameter("@Wait",Wait)
                                           };
            SqlHelper.ExecuteNonQuery(InsertSql, sqlParameters);
        }
    }
}
